<?php
/**
 * The framework configuration file.  Changes are unlikely necessary here.
 * @package KvFramework_v2
 * @author Greg McWhirter <gsmcwhirter@gmail.com>
 * @copyright Copyright &copy; 2006, Greg McWhirter
 * @version $Revision: 25M $
 */

if(defined("USE_OB_GZHANDLER") && USE_OB_GZHANDLER)
{
  ob_start("ob_gzhandler");
}
else
{
  ob_start();
}
session_start();
date_default_timezone_set("America/New_York");

if(defined("SUPER_DEBUG") && SUPER_DEBUG)
{
  print "<pre>";
  print_r($_SERVER);
  print "</pre>";
}

try
{
/**
 * Flag for whether the session has been started
 * @global boolean
 */
$SESSIONSTARTED = true;

/**
 * Starting micro timestamp
 * @global float
 */
$_STARTTIME = microtime(true);

/**
 * Flag for whether a MIME type has been set or not
 * @global boolean
 */
$MIMESET = false;

/**
 * Application filesystem root directory constant
 */
define("ROOTDIR", dirname(__FILE__)."/..");

/**
 * Framwork filesystem directory constant
 */
define("KVF_FRAMEWORK", ROOTDIR."/kvframework"); //no trailing slash

/**
 * Application includes filesystem directory constant
 */
define("KVF_INCLUDES", ROOTDIR."/includes"); //no trailing slash

/**
 * Application modules filesystem directory constant
 */
define("KVF_MODULES", KVF_INCLUDES."/modules");

/**
 * Application layouts filesystem directory constant
 */
define("KVF_LAYOUTS_DIR", KVF_INCLUDES."/layouts");

/**
 * Application models filesystem directory constant
 */
define("KVF_MODELS", KVF_INCLUDES."/models");

/**
 * Global inflector instance
 * @global KvfBaseInflector
 */
$INFLECTOR = null;

define_syslog_variables();
/**#@+
 * Logger level constant
 */
define ("KVF_LOG_LEMERG", LOG_EMERG);
define ("KVF_LOG_LALERT", LOG_ALERT);
define ("KVF_LOG_LCRITICAL", LOG_CRIT);
define ("KVF_LOG_LERROR", LOG_ERR);
define ("KVF_LOG_LWARNING", LOG_WARNING);
define ("KVF_LOG_LNOTICE", LOG_NOTICE);
define ("KVF_LOG_LINFO", LOG_INFO);
define ("KVF_LOG_LDEBUG", LOG_DEBUG);
/**#@-*/

require_once KVF_INCLUDES."/configs/framework.config.php";

if(!class_exists("CONFIG"))
{
  throw new Exception("CONFIG class not found.");
}

/**
 * A wrapper for including files and logging such. Uses func_get_args() for variable argument number.
 * @return boolean true
 * @throws Exception
 */
function load_files()
{
  $all_okay = true;
  $args = func_get_args();
  if(count($args) > 0 && is_bool($args[0]))
  {
    $autoload = array_shift($args);
  }
  else
  {
    $autoload = false;
  }

  foreach($args as $filename)
  {
    if(!(@include_once $filename))
    {
      $all_okay = false;
      if(defined("KVF_LOGGING") && KVF_LOGGING)
      {
        /* TODO: add logging */
        KvfLoghandler::Write("Failed loading file: ".$filename);
      }

      if(!$autoload)
      {
        throw new Exception("Load files failed for ".$filename);
      }
      else
      {
        throw new Exception("AutoLoad files failed for ".$filename);
      }
      return false;
    }

    if(defined("KVF_LOGGING") && KVF_LOGGING)
    {
      /* TODO: add logging */
      KvfLoghandler::Write("Loading file: ".$filename, KVF_LOG_LDEBUG);
    }
  }

  return true;
}

/**
 * PHP Magic autoload function for classes.  Set to only work on module, action, and model files.
 * @param string $class Name of the module to be loaded.
 * @global KvfBaseInflector The inflector
 * @return boolean Return the result of the load_files call or false if the requested class was not a module
 */
function __autoload($class)
{
  global $INFLECTOR;

  if(!$INFLECTOR instanceOf KvfBaseInflector)
  {
    $INFLECTOR = new KvfBaseInflector();
  }

  $class = $INFLECTOR->Camel2Underscore($class);
  if(substr($class,-6) == "module")
  {
    $f = KVF_MODULES."/".substr($class,0,-7).".module/".substr($class,0,-7).".module.php";
    if(load_files(true, $f))
    {
      return true;
    }
    else
    {
      $e = new Exception("Autoload failed for file ".$f);
      include KVF_FRAMEWORK."/exception.phtml";
      exit(0);
    }
  }
  elseif(substr($class, -6) == "action")
  {
    list($mod, $act) = explode("module", $class, 2);
    $f = KVF_MODULES."/".substr($mod,0,-1).".module/actions/".substr($act,1,-7).".action.php";
    if(load_files(true, $f))
    {
      return true;
    }
    else
    {
      $e = new Exception("Autoload failed for file ".$f);
      include KVF_FRAMEWORK."/exception.phtml";
      exit(0);
    }
  }
  elseif(substr($class, -5) == "model")
  {
    $f = KVF_MODELS."/".substr($class,0,-6).".model.php";
    if(load_files(true, $f))
    {
      return true;
    }
    else
    {
      $e = new Exception("Autoload failed for file ".$f);
      include KVF_FRAMEWORK."/exception.phtml";
      exit(0);
    }
  }
  else
  {
    $e = new Exception("Autoload failed for class ".$class);
    include KVF_FRAMEWORK."/exception.phtml";
    exit(0);
  }
}

load_files(
  KVF_FRAMEWORK."/lib/nakor_core.class.php",
  KVF_FRAMEWORK."/lib/functions.lib.php",
  KVF_FRAMEWORK."/kvf_base/kvf_base.class.php"
);

if(CONFIG::USE_LOGHANDLER)
{
  /**
   * Framework logging constant
   */
  load_files(KVF_FRAMEWORK."/kvf_loghandler/kvf_loghandler.class.php");
  define("KVF_LOGGING", true);
  if(!class_exists("CONFIG_LOGHANDLER"))
  {
    throw new Exception("Loghandler configuration not found.");
  }

  $LOGINFO = new KvfLoginfoStruct();
  $LOGINFO->types = CONFIG_LOGHANDLER::$TYPES;
  $LOGINFO->configs = CONFIG_LOGHANDLER::$CONFIGS;
  $LOGINFO->default = CONFIG_LOGHANDLER::$DEFAULT;

  KvfLoghandler::Start($LOGINFO);
}
else
{
  /**
   * @ignore
   */
  define("KVF_LOGGING", false);
}

load_files(
  KVF_FRAMEWORK."/kvf_router/kvf_router.class.php",
  KVF_FRAMEWORK."/kvf_modulehandler/kvf_module.class.php",
  KVF_FRAMEWORK."/kvf_markup/kvf_markup.class.php"
);

if(CONFIG::USE_CACHE)
{
  if(!class_exists("CONFIG_CACHE"))
  {
    throw new Exception("Cache configuration not found.");
  }
  load_files(KVF_FRAMEWORK."/kvf_cache/kvf_cache.class.php");
}

if(CONFIG::USE_DBHANDLER)
{
  if(!class_exists("CONFIG_DBHANDLER"))
  {
    throw new Exception("Dbhandler configuration not found.");
  }
  load_files(KVF_FRAMEWORK."/kvf_dbhandler/kvf_dbhandler.class.php");

  $DB = new KvfDbhandlerMysql(array(
    "dbname" => CONFIG_DBHANDLER::DBNAME,
    "dbuser" => CONFIG_DBHANDLER::USER,
    "dbpass" => CONFIG_DBHANDLER::PASS,
    "dbhost" => CONFIG_DBHANDLER::HOST
  ));

}
else
{
  $DB = null;
}

if(CONFIG::USE_MAILHANDLER)
{
  if(!class_exists("CONFIG_MAILHANDLER"))
  {
    throw new Exception("Mailhandler configuration not found.");
  }
  load_files(KVF_FRAMEWORK."/kvf_mailhandler/kvf_mailhandler.class.php");

  KvfMailhandler::Configure(
    CONFIG_MAILHANDLER::SEND_MAIL,
    CONFIG_MAILHANDLER::FROM_ADDR,
    CONFIG_MAILHANDLER::FROM_NAME
  );

}

load_files(
  KVF_INCLUDES."/configs/application.config.php"
);

if (!defined("KVFURI"))
{
  define("KVFURI", "/");
}

KvfBase::RunApplication($DB, KVFURI);
}
catch(Exception $e)
{
  include KVF_FRAMEWORK."/exception.phtml";
}

KvfBase::SaveMessages();
//TODO: Add logging a lot of places
if(!$MIMESET)
{
  //SetMime("application/xhtml+xml");
  SetMime("text/html");
}
if(defined("KVF_LOGGING") && KVF_LOGGING)
{
  KvfLoghandler::Close();
}

session_commit();
ob_end_flush();
exit(0);

function DoRedirect($url)
{
  KvfBase::SaveMessages();
  session_commit();
  if(defined("KVF_LOGGING") && KVF_LOGGING)
  {
    KvfLoghandler::Close();
  }
  header("Location: ".$url);
  ob_end_flush();
  exit(0);
}

function SetMime($type, $force = false)
{
  global $MIMESET;

  if($MIMESET && !$force)
  {
    return false;
  }
  else
  {
    header("Content-type: ".$type."\r\n");
    $MIMESET = true;
  }
}
?>
